package com.lihao.baselibrary.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.jaeger.library.StatusBarUtil;
import com.lihao.baselibrary.R;
import com.lihao.baselibrary.utils.AppManager;
import com.lihao.baselibrary.utils.KeyboardUtils;
import com.lihao.baselibrary.utils.T;
import com.lihao.baselibrary.weight.LoadingDialog;

import butterknife.ButterKnife;


/**
 * Created by lihao on 2017/8/1.
 */
public abstract class BaseActivity extends AppCompatActivity implements baseView{


    public LoadingDialog dialog;
    public static Activity context;
    public Intent intent;
    public static ColumValue value;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        if (getContentViewId() != 0) {
            setContentView(getContentViewId());
        }

        ButterKnife.bind(this);
        dialog = new LoadingDialog(this, R.style.DialogStyle);
        context = this;
        intent = new Intent();
        value =new ColumValue(this);


        initView();
        initListener();
        initData();




    }
    protected abstract int getContentViewId();

    protected abstract void initData();

    protected abstract void initView();

    protected abstract void initListener();
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        setStatusBar(R.color.colorPrimary);
    }

    /**
     * 更改界面状态栏则直接重写此方法
     *
     * @param color
     */
    protected void setStatusBar(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusBarUtil.setColor(this, getResources().getColor(color), 0);
            setDarkStatusIcon(true);
        } else {
            StatusBarUtil.setColor(this, getResources().getColor(color), 30);
        }
    }
    /**
     * 是否使用深色状态栏
     *
     * @param bDark
     */
    protected void setDarkStatusIcon(boolean bDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = getWindow().getDecorView();
            if (decorView != null) {
                int vis = decorView.getSystemUiVisibility();
                if (bDark) {
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else {
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }


    @Override
    public void showToast(String msg) {
        T.showShort(context,msg);
    }

    @Override
    public void showToast(int msg_ID) {
        T.showShort(context,getString(msg_ID));
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void dismissDialog() {
        dialog.dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KeyboardUtils.hideSoftInput(this);
        AppManager.getAppManager().finishActivity(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        KeyboardUtils.hideSoftInput(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        context = this;
    }
}
